home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicCheckBoxMenuItemUI.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  199 lines

  1. /*
  2.  * @(#)BasicCheckBoxMenuItemUI.java    1.41 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.*;
  26. import com.sun.java.swing.plaf.*;
  27. import com.sun.java.swing.border.*;
  28. import java.io.Serializable;
  29.  
  30.  
  31. /**
  32.  * BasicCheckboxMenuItem implementation
  33.  * <p>
  34.  * Warning: serialized objects of this class will not be compatible with
  35.  * future swing releases.  The current serialization support is appropriate 
  36.  * for short term storage or RMI between Swing1.0 applications.  It will
  37.  * not be possible to load serialized Swing1.0 objects with future releases
  38.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  39.  * baseline for the serialized form of Swing objects.
  40.  *
  41.  * @version 1.41 02/02/98
  42.  * @author Georges Saab
  43.  * @author David Karlton
  44.  * @author Arnaud Weber
  45.  */
  46. public class BasicCheckBoxMenuItemUI extends CheckBoxMenuItemUI implements Serializable
  47. {
  48.     protected static Color pressedBackground = null;
  49.     protected static Color pressedForeground = null;
  50.     protected Icon menuArrow = null;
  51.     protected Icon checkIcon = null;
  52.  
  53.     // visual constants
  54.     protected static final int defaultTextIconGap = 4;
  55.  
  56.     protected MouseListener       mouseListener;
  57.     protected MouseMotionListener mouseMotionListener;
  58.  
  59.     protected boolean oldBorderPainted;
  60.  
  61.     public static ComponentUI createUI(JComponent c) {
  62.         return new BasicCheckBoxMenuItemUI();
  63.     }
  64.  
  65.     public void installUI(JComponent c) {
  66.     JCheckBoxMenuItem menuItem = (JCheckBoxMenuItem) c;
  67.     initListeners(c);
  68.     addListeners(c);
  69.     // Set defaults
  70.     c.setOpaque(true);
  71.     LookAndFeel.installBorder(c,"MenuItem.border");
  72.     oldBorderPainted = menuItem.isBorderPainted();
  73.     menuItem.setBorderPainted( ( (Boolean) (UIManager.get("MenuItem.borderPainted")) ).booleanValue() );
  74.     LookAndFeel.installColorsAndFont(c,
  75.                           "MenuItem.background",
  76.                           "MenuItem.foreground",
  77.                           "MenuItem.font");
  78.     // MenuItem specific defaults
  79.     if (pressedBackground == null || 
  80.         pressedBackground instanceof UIResource) {
  81.         pressedBackground = 
  82.         UIManager.getColor("MenuItem.pressedBackground");
  83.     }
  84.     if (pressedForeground == null || 
  85.         pressedForeground instanceof UIResource) {
  86.         pressedForeground = 
  87.         UIManager.getColor("MenuItem.pressedForeground");
  88.     }
  89.     if (menuArrow == null ||
  90.         menuArrow instanceof UIResource) {
  91.         menuArrow = UIManager.getIcon("MenuItem.arrowIcon");
  92.     }
  93.     if (menuItem.getSelectedIcon() == null ||
  94.         menuItem.getSelectedIcon() instanceof UIResource) {
  95.         menuItem.setSelectedIcon(UIManager.getIcon("CheckBoxMenuItem.icon"));
  96.     }
  97.     }
  98.  
  99.     public void uninstallUI(JComponent c) {
  100.     JCheckBoxMenuItem menuItem = (JCheckBoxMenuItem) c;
  101.     removeListeners(c);
  102.     LookAndFeel.uninstallBorder(c);
  103.     ((JMenuItem) c).setBorderPainted( oldBorderPainted );
  104.         if (menuItem.getSelectedIcon() instanceof UIResource) {
  105.             menuItem.setSelectedIcon(null);
  106.         }
  107.     if (menuArrow instanceof UIResource)
  108.         menuArrow = null;
  109.     if (checkIcon instanceof UIResource)
  110.         checkIcon = null;
  111.     }
  112.  
  113.     protected void initListeners(JComponent c) {
  114.         mouseListener = createMouseListener(c);
  115.         mouseMotionListener  = createMouseMotionListener(c);
  116.     }
  117.  
  118.     protected void addListeners(JComponent c) {
  119.         c.addMouseListener(mouseListener);    
  120.         c.addMouseMotionListener(mouseMotionListener);
  121.     }
  122.  
  123.     protected void removeListeners(JComponent c) {
  124.         c.removeMouseListener(mouseListener);
  125.         c.removeMouseMotionListener(mouseMotionListener);
  126.     }
  127.  
  128.     protected MouseListener createMouseListener(JComponent c) {
  129.     return new BasicMenuMouseListener();
  130.     }
  131.  
  132.     protected MouseMotionListener createMouseMotionListener(JComponent c) {
  133.         return new BasicMenuMouseMotionListener();
  134.     }
  135.  
  136.     public Dimension getMinimumSize(JComponent c) {
  137.         return getPreferredSize(c);
  138.     }
  139.  
  140.     public Dimension getPreferredSize(JComponent c) {
  141.     return BasicGraphicsUtils.getPreferredMenuItemSize(c,
  142.                                checkIcon, 
  143.                                menuArrow, 
  144.                                defaultTextIconGap);
  145.     }
  146.  
  147.     public Dimension getMaximumSize(JComponent c) {
  148.         return getPreferredSize(c);
  149.     }
  150.  
  151.     public Insets getDefaultMargin(AbstractButton c) { 
  152.         return new Insets(2,2,2,2);
  153.     }
  154.  
  155.  
  156.     /**
  157.      * We draw the background in BasicGraphicsUtils.paintMenuItem()
  158.      * so override update (which fills the background of opaque
  159.      * components by default) to just call paint().
  160.      *
  161.      * @see BasicGraphicsUtils#paintMenuItem
  162.      */
  163.     public void update(Graphics g, JComponent c) {
  164.         paint(g, c);
  165.     }
  166.  
  167.     public void paint(Graphics g, JComponent c) {
  168.     BasicGraphicsUtils.paintMenuItem(g, c, ((JCheckBoxMenuItem)c).getSelectedIcon(), menuArrow,
  169.                      pressedBackground, pressedForeground,
  170.                      defaultTextIconGap);    
  171.     }
  172.  
  173.     public void processMouseEvent(JMenuItem item,MouseEvent e,MenuElement path[],MenuSelectionManager manager) {
  174.         Point p = e.getPoint();
  175.         if(p.x >= 0 && p.x < item.getWidth() &&
  176.            p.y >= 0 && p.y < item.getHeight()) {
  177.             if(e.getID() == MouseEvent.MOUSE_RELEASED) {
  178.                 manager.clearSelectedPath();
  179.                 item.doClick(0);
  180.             } else
  181.                 manager.setSelectedPath(path);
  182.         } else if(item.getModel().isArmed()) {
  183.             MenuElement newPath[] = new MenuElement[path.length-1];
  184.             int i,c;
  185.             for(i=0,c=path.length-1;i<c;i++)
  186.                 newPath[i] = path[i];
  187.             manager.setSelectedPath(newPath);
  188.         }
  189.     }
  190. }
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.